package vn.edu.saigontech.ncms.web.offering;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;

import vn.edu.saigontech.ncms.model.enumeration.TeacherType;
import vn.edu.saigontech.ncms.model.enumeration.TeachingInterruptionReason;
import vn.edu.saigontech.ncms.model.info.Offering;
import vn.edu.saigontech.ncms.model.info.Teacher;
import vn.edu.saigontech.ncms.model.teaching.TeachingInterruption;
import vn.edu.saigontech.ncms.model.teaching.TeachingSwitch;
import vn.edu.saigontech.ncms.service.OfferingService;
import vn.edu.saigontech.ncms.service.TeacherService;
import vn.edu.saigontech.ncms.service.TeachingInterruptionService;
import vn.edu.saigontech.ncms.service.UserService;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class TeachingInterruptionAction extends BaseAction {
	
	private Long id;
	private Long offeringId;
	private Long teacherId;
	private String fromDate;
	private String toDate;
	private String reason;
	private Long substituteTeacherId;
	private Long staffId;
	private String makeupFromDate;
	private String makeupToDate;
	private String comment;
	
	public String showAdd() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		
		Collection<Teacher> mainTeachers = offering.getInstructors();
		Collection<Teacher> subTeachers = getSubtituteTeachers(offering);
		
		Collection<Teacher> mainTAs = offering.getTAs();
		Collection<Teacher> subTAs = getSubtituteTAs(offering);

		Collection<Teacher> teachers = TeacherService.INSTANCE.findActiveByTeacherType(TeacherType.INSTRUCTOR);
		Collection<Teacher> tas = TeacherService.INSTANCE.findActiveByTeacherType(TeacherType.TA);
		
		REQUEST().setAttribute("teacher", TeacherService.INSTANCE.findById(this.teacherId));
		REQUEST().setAttribute("offering", offering);
		
		REQUEST().setAttribute("mainTeachers", mainTeachers);
		REQUEST().setAttribute("subTeachers", subTeachers);
		
		REQUEST().setAttribute("mainTAs", mainTAs);
		REQUEST().setAttribute("subTAs", subTAs);
		
		REQUEST().setAttribute("teachers", teachers);
		REQUEST().setAttribute("tas", tas);
		
		REQUEST().setAttribute("staffs", UserService.INSTANCE.findByRole("Supervisor"));
		return SUCCESS;
	}
	
	public String add() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		Date fromDateDate = DateUtil.fromString(this.fromDate, this.i18n("format.date"));
		Date toDateDate = DateUtil.fromString(this.toDate, this.i18n("format.date"));
		
		TeachingInterruption interruption = new TeachingInterruption();
		interruption.setOffering(offering);
		interruption.setFromDate(fromDateDate);
		interruption.setToDate(toDateDate);
		
		Teacher teacher = TeacherService.INSTANCE.findById(this.teacherId);
		interruption.setTeacher(teacher);
		interruption.setReason(TeachingInterruptionReason.create(this.reason));
		if(TeachingInterruptionReason.CANCEL.equals(TeachingInterruptionReason.create(this.reason))) {
			interruption.setMakeupFromDate(DateUtil.fromString(this.makeupFromDate, this.i18n("format.date")));
			interruption.setMakeupToDate(DateUtil.fromString(this.makeupToDate, this.i18n("format.date")));
		}
		else if(TeachingInterruptionReason.SUBSTITUTE.equals(TeachingInterruptionReason.create(this.reason))) {
			interruption.setSubstituteTeacher(TeacherService.INSTANCE.findById(this.substituteTeacherId));
		}
		else {
			interruption.setStaff(UserService.INSTANCE.findById(this.staffId));
		}
		interruption.setComment(this.comment);
		TeachingInterruptionService.INSTANCE.persist(interruption);
		this.offeringId = offering.getId();
		
		this.addLoggingDetail(BaseAction.ADD, "logging.interruption.add", teacher.getFullName(), offering.getName());
		return SUCCESS;
	}
	
	public String showEdit() {
		TeachingInterruption interruption = TeachingInterruptionService.INSTANCE.findById(this.id);
		Offering offering = interruption.getOffering();
		Collection<Teacher> mainTeachers = offering.getInstructors();
		Collection<Teacher> subTeachers = getSubtituteTeachers(offering);;
		
		Collection<Teacher> mainTAs = offering.getTAs();
		Collection<Teacher> subTAs = getSubtituteTAs(offering);
		
		Collection<Teacher> teachers = TeacherService.INSTANCE.findActiveByTeacherType(TeacherType.INSTRUCTOR);
		Collection<Teacher> tas = TeacherService.INSTANCE.findActiveByTeacherType(TeacherType.TA);
		
		REQUEST().setAttribute("model", interruption);
		REQUEST().setAttribute("staffs", UserService.INSTANCE.findAllByRole("Supervisor"));
		
		REQUEST().setAttribute("mainTeachers", mainTeachers);
		REQUEST().setAttribute("subTeachers", subTeachers);
		
		REQUEST().setAttribute("mainTAs", mainTAs);
		REQUEST().setAttribute("subTAs", subTAs);
		
		REQUEST().setAttribute("teachers", teachers);
		REQUEST().setAttribute("tas", tas);
		return SUCCESS;
	}
	
	public String edit() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		Date fromDateDate = DateUtil.fromString(this.fromDate, this.i18n("format.date"));
		Date toDateDate = DateUtil.fromString(this.toDate, this.i18n("format.date"));
		
		TeachingInterruption interruption = TeachingInterruptionService.INSTANCE.findById(this.id);
		String originalTeacher = interruption.getTeacher().getFullName();
		interruption.setFromDate(fromDateDate);
		interruption.setToDate(toDateDate);
		interruption.setTeacher(TeacherService.INSTANCE.findById(this.teacherId));
		interruption.setReason(TeachingInterruptionReason.create(this.reason));
		if(TeachingInterruptionReason.CANCEL.equals(TeachingInterruptionReason.create(this.reason))) {
			interruption.setMakeupFromDate(DateUtil.fromString(this.makeupFromDate, this.i18n("format.date")));
			interruption.setMakeupToDate(DateUtil.fromString(this.makeupToDate, this.i18n("format.date")));
			interruption.setSubstituteTeacher(null);
			interruption.setStaff(null);
		}
		else if(TeachingInterruptionReason.SUBSTITUTE.equals(TeachingInterruptionReason.create(this.reason))) {
			interruption.setSubstituteTeacher(TeacherService.INSTANCE.findById(this.substituteTeacherId));
			interruption.setMakeupFromDate(null);
			interruption.setMakeupToDate(null);
			interruption.setStaff(null);
		}
		else {
			interruption.setStaff(UserService.INSTANCE.findById(this.staffId));
			interruption.setSubstituteTeacher(null);
			interruption.setMakeupFromDate(null);
			interruption.setMakeupToDate(null);
		}
		interruption.setComment(this.comment);
		TeachingInterruptionService.INSTANCE.persist(interruption);
		this.offeringId = offering.getId();
		
		this.addLoggingDetail(BaseAction.EDIT, "logging.interruption.edit", originalTeacher, offering.getName());
		return SUCCESS;
	}
	
	public String list() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		if(offering.getTeachingInterruptions().isEmpty())
			return ADD;
		REQUEST().setAttribute("offering", offering);
		return SUCCESS;
	}
	
	public String delete() {
		TeachingInterruption interruption = TeachingInterruptionService.INSTANCE.findById(this.id);
		String offeringName = interruption.getOffering().getName();
		String teacherName = interruption.getTeacher().getFullName();
		TeachingInterruptionService.INSTANCE.deleteById(this.id);
		
		this.addLoggingDetail(BaseAction.DELETE, "logging.interruption.delete", teacherName, offeringName);
		return SUCCESS;
	}
	
	private List<Teacher> getSubtituteTeachers(Offering offering) {
		List<Teacher> ret = new ArrayList<Teacher>();
		Set<TeachingSwitch> switchs = offering.getTeachingSwitchs();
		for(TeachingSwitch item : switchs) {
			Teacher inplaceTeacher = item.getInplaceTeacher();
			if(!ret.contains(inplaceTeacher)) {
				ret.add(inplaceTeacher);
			}
		}
		List<TeachingInterruption> interruptions = offering.getInstructorInterruptions();
		for (TeachingInterruption interruption : interruptions) {
			if (interruption.getReason().equals(TeachingInterruptionReason.SUBSTITUTE)) {
				Teacher subTeacher = interruption.getSubstituteTeacher();
				if (!ret.contains(subTeacher)) {
					ret.add(subTeacher);
				}
			}
		}
		return ret;
	}
	
	private List<Teacher> getSubtituteTAs(Offering offering) {
		List<Teacher> ret = new ArrayList<Teacher>();
		List<TeachingInterruption> interruptions = offering.getTAInterruptions();
		for (TeachingInterruption interruption : interruptions) {
			if (interruption.getReason().equals(TeachingInterruptionReason.SUBSTITUTE)) {
				Teacher subTeacher = interruption.getSubstituteTeacher();
				if (!ret.contains(subTeacher)) {
					ret.add(subTeacher);
				}
			}
		}
		return ret;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getOfferingId() {
		return offeringId;
	}

	public void setOfferingId(Long offeringId) {
		this.offeringId = offeringId;
	}

	public String getFromDate() {
		return fromDate;
	}

	public void setFromDate(String fromDate) {
		this.fromDate = fromDate;
	}

	public String getToDate() {
		return toDate;
	}

	public void setToDate(String toDate) {
		this.toDate = toDate;
	}

	public String getReason() {
		return reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}

	public Long getSubstituteTeacherId() {
		return substituteTeacherId;
	}

	public void setSubstituteTeacherId(Long substituteTeacherId) {
		this.substituteTeacherId = substituteTeacherId;
	}

	public String getMakeupFromDate() {
		return makeupFromDate;
	}

	public void setMakeupFromDate(String makeupFromDate) {
		this.makeupFromDate = makeupFromDate;
	}

	public String getMakeupToDate() {
		return makeupToDate;
	}

	public void setMakeupToDate(String makeupToDate) {
		this.makeupToDate = makeupToDate;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public Long getTeacherId() {
		return teacherId;
	}

	public void setTeacherId(Long teacherId) {
		this.teacherId = teacherId;
	}

	public Long getStaffId() {
		return staffId;
	}

	public void setStaffId(Long staffId) {
		this.staffId = staffId;
	}
}
